package com.cn.kehong.service;

import com.cn.kehong.api.IThroughRateDetailService;
import com.cn.kehong.api.IThroughRateService;
import com.cn.kehong.dao.ThroughRateMapper;
import com.cn.kehong.domain.ThroughRate;
import com.cn.kehong.domain.ThroughRateDetail;
import com.cn.kehong.domain.ThroughRateExample;
import com.cn.kehong.domain.ThroughRateVo;
import com.cn.kehong.utils.CommonUtil;
import com.cn.kehong.utils.Constants;
import com.cn.kehong.utils.NumberUtil;
import com.cn.kehong.utils.QueryForm;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class ThroughRateServiceImpl implements IThroughRateService {

    @Autowired
    private ThroughRateMapper throughRateMapper;

    @Autowired
    private IThroughRateDetailService throughRateDetailService;

    @Override
    public List<ThroughRate> getThroughRateList(QueryForm queryForm) throws Exception {
        int count = throughRateMapper.selectThroughRateListCount(queryForm);
        if (count == 0) {
            return Lists.newArrayList();
        }
        List<ThroughRate> throughRates = throughRateMapper.selectThroughRateList(queryForm);
        throughRates.get(0).setCount(count);
        return throughRates;
    }

    @Override
    public List<ThroughRateVo> getThroughRateVoList(QueryForm queryForm) throws Exception {
        String factoryName = queryForm.getFactoryName()+"-";
        String date = queryForm.getThroughTime();
        if(StringUtils.isEmpty(factoryName)||StringUtils.isEmpty(date)){
            return Lists.newArrayList();
        }
        List<ThroughRate> throughRates = throughRateMapper.selectThroughRateWithDetail(queryForm);
        if(CollectionUtils.isEmpty(throughRates)){
            return Lists.newArrayList();
        }

        Map<String,ThroughRateDetail> rateMap = Maps.newLinkedHashMap();
        rateMap.put("A",new ThroughRateDetail());
        rateMap.put("B",new ThroughRateDetail());
        rateMap.put("C",new ThroughRateDetail());
        rateMap.put("S",new ThroughRateDetail());
        rateMap.put("Y",new ThroughRateDetail());

        List<ThroughRateVo> results = Lists.newLinkedList();
        for(ThroughRate throughRate : throughRates){
            List<ThroughRateDetail> details = throughRate.getThroughRateDetails();
            if(CollectionUtils.isEmpty(details)){
                continue;
            }
            String factory = throughRate.getFactoryName();
            for(ThroughRateDetail rate : details){
                String gradeValue = rate.getGradeValue();
                ThroughRateVo vo = new ThroughRateVo();
                vo.setCj(factory);
                vo.setNames(Lists.newArrayList(gradeValue));

                double dp1 = 0.00;
                double dp2 = 0.00;
                List<String> mas = Lists.newLinkedList();
                mas.add(rate.getMa());
                mas.add(rate.getExtend1());
                dp1 +=Double.parseDouble(rate.getMa());
                dp2 +=Double.parseDouble(rate.getExtend1());
                dp1 +=Double.parseDouble(rate.getMb());
                dp2 +=Double.parseDouble(rate.getExtend2());
                dp1 +=Double.parseDouble(rate.getMc());
                dp2 +=Double.parseDouble(rate.getExtend3());
                dp1 +=Double.parseDouble(rate.getMd());
                dp2 +=Double.parseDouble(rate.getExtend4());

                if(rateMap.containsKey(gradeValue)){
                    ThroughRateDetail exist = rateMap.get(gradeValue);
                    if(StringUtils.isEmpty(exist.getGradeValue())){
                        exist.setGradeValue(rate.getGradeValue());
                        exist.setMa(rate.getMa());
                        exist.setMb(rate.getMb());
                        exist.setMc(rate.getMc());
                        exist.setMd(rate.getMd());
                        exist.setExtend1(rate.getExtend1());
                        exist.setExtend2(rate.getExtend2());
                        exist.setExtend3(rate.getExtend3());
                        exist.setExtend4(rate.getExtend4());
                    }else{
                        double addMa = Double.parseDouble(rate.getMa())+ Double.parseDouble(exist.getMa());
                        double addMb = Double.parseDouble(rate.getMb())+ Double.parseDouble(exist.getMb());
                        double addMc = Double.parseDouble(rate.getMc())+ Double.parseDouble(exist.getMc());
                        double addMd = Double.parseDouble(rate.getMc())+ Double.parseDouble(exist.getMd());
                        double addExtend1 = Double.parseDouble(rate.getExtend1())+ Double.parseDouble(exist.getExtend1());
                        double addExtend2 = Double.parseDouble(rate.getExtend2())+ Double.parseDouble(exist.getExtend2());
                        double addExtend3 = Double.parseDouble(rate.getExtend3())+ Double.parseDouble(exist.getExtend3());
                        double addExtend4 = Double.parseDouble(rate.getExtend4())+ Double.parseDouble(exist.getExtend4());
                        exist.setMa(addMa+"");
                        exist.setMb(addMb+"");
                        exist.setMc(addMc+"");
                        exist.setMd(addMd+"");
                        exist.setExtend1(addExtend1+"");
                        exist.setExtend2(addExtend2+"");
                        exist.setExtend3(addExtend3+"");
                        exist.setExtend4(addExtend4+"");
                    }
                }

                if(Double.parseDouble(rate.getMa()) == 0){
                    mas.add("0.00%");
                }else{
                    double v1 = Double.parseDouble(rate.getMa());
                    double v2 = Double.parseDouble(rate.getExtend1());
                    String percent = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                    mas.add(percent);
                }
                vo.setMas(mas);

                List<String> mbs = Lists.newLinkedList();
                mbs.add(rate.getMb());
                mbs.add(rate.getExtend2());
                if(Double.parseDouble(rate.getMb()) == 0){
                    mbs.add("0.00%");
                }else{
                    double v1 = Double.parseDouble(rate.getMb());
                    double v2 = Double.parseDouble(rate.getExtend2());
                    String percent = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                    mbs.add(percent);
                }
                vo.setMbs(mbs);

                List<String> mcs = Lists.newLinkedList();
                mcs.add(rate.getMc());
                mcs.add(rate.getExtend3());
                if(Double.parseDouble(rate.getMc()) == 0){
                    mcs.add("0.00%");
                }else{
                    double v1 = Double.parseDouble(rate.getMc());
                    double v2 = Double.parseDouble(rate.getExtend3());
                    String percent = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                    mcs.add(percent);
                }
                vo.setMcs(mcs);

                List<String> mds = Lists.newLinkedList();
                mds.add(rate.getMd());
                mds.add(rate.getExtend4());
                if(Double.parseDouble(rate.getMd()) == 0){
                    mds.add("0.00%");
                }else{
                    double v1 = Double.parseDouble(rate.getMd());
                    double v2 = Double.parseDouble(rate.getExtend4());
                    String percent = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                    mds.add(percent);
                }
                vo.setMds(mds);

                if(dp1 == 0){
                    vo.setDp("0.00%");
                }else{
                    String dpPercent = NumberUtil.getPoint(dp2*100.0/dp1,2)+"%";
                    vo.setDp(dpPercent);
                }
                results.add(vo);
            }
        }
        ThroughRateVo vo = new ThroughRateVo();
        vo.setCj(queryForm.getFactoryName());
        List<String> names = Lists.newLinkedList();
        List<String> mas = Lists.newLinkedList();
        List<String> mbs = Lists.newLinkedList();
        List<String> mcs = Lists.newLinkedList();
        List<String> mds = Lists.newLinkedList();
        List<String> zps = Lists.newLinkedList();
        for(String key:rateMap.keySet()){
            ThroughRateDetail detail = rateMap.get(key);
            if(StringUtils.isEmpty(detail.getGradeValue())){
                names.add("");
                mas.add("");
                mbs.add("");
                mcs.add("");
                mds.add("");
                zps.add("");
            }else {
                names.add(key);
                double total1 = 0.00;
                double total2 = 0.00;

                double v1 = Double.parseDouble(detail.getMa());
                double v2 = Double.parseDouble(detail.getExtend1());
                total1+=v1;
                total2+=v2;

                if(v1 == 0){
                    mas.add("0.00%");
                }else{
                    String dist = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                    mas.add(dist);
                }

                double v3 = Double.parseDouble(detail.getMb());
                double v4 = Double.parseDouble(detail.getExtend2());
                total1+=v3;
                total2+=v4;

                if(v3 == 0){
                    mbs.add("0.00%");
                }else{
                    String dist = NumberUtil.getPoint(v4*100.0/v3,2)+"%";
                    mbs.add(dist);
                }

                double v5 = Double.parseDouble(detail.getMc());
                double v6 = Double.parseDouble(detail.getExtend3());

                total1+=v5;
                total2+=v6;
                if(v5 == 0){
                    mcs.add("0.00%");
                }else{
                    String dist = NumberUtil.getPoint(v6*100.0/v5,2)+"%";
                    mcs.add(dist);
                }

                double v7 = Double.parseDouble(detail.getMd());
                double v8 = Double.parseDouble(detail.getExtend4());
                total1+=v7;
                total2+=v8;
                if(v7 == 0){
                    mds.add("0.00%");
                }else{
                    String dist = NumberUtil.getPoint(v8*100.0/v7,2)+"%";
                    mds.add(dist);
                }

                if(v7 == 0){
                    zps.add("0.00%");
                }else{
                    String dist = NumberUtil.getPoint(total2*100.0/total1,2)+"%";
                    zps.add(dist);
                }
            }
        }
        vo.setMas(mas);
        vo.setMbs(mbs);
        vo.setMcs(mcs);
        vo.setMds(mds);
        vo.setDps(zps);
        vo.setNames(names);
        results.add(vo);
        return results;
    }

    @Override
    public List<String> getThroughRateTimes() throws Exception {
        return throughRateMapper.selectThroughRateTime();
    }

    @Override
    public String updateThroughRate(ThroughRate throughRate) throws Exception {
        String id = throughRate.getId();
        if(StringUtils.isEmpty(id)){
            return "主键为空！";
        }
        List<ThroughRateDetail> throughRateDetails = throughRate.getThroughRateDetails();
        if(CollectionUtils.isEmpty(throughRateDetails)){
            return "数据异常！";
        }
        Map<String,Integer> gradeMap = Maps.newHashMap();
        for(ThroughRateDetail detail:throughRateDetails){
            if(gradeMap.containsKey(detail.getGradeValue())){
                return "不能存在相同规格！";
            }
            gradeMap.put(detail.getGradeValue(),1);
        }
        ThroughRateExample example = new ThroughRateExample();
        example.createCriteria().andIdEqualTo(id);

        int count = throughRateMapper.updateByExampleSelective(throughRate,example);
        if(count < 1){
            return "更新数据异常！";
        }
        String resp = throughRateDetailService.updateThroughRateDetails(throughRateDetails);
        if(StringUtils.isNotEmpty(resp)){
            throw new Exception(resp);
        }
        return resp;
    }

    @Override
    public String addThroughRate(ThroughRate throughRate) throws Exception {
        String id = CommonUtil.generateRandomNum("rate-");
        String date = throughRate.getThroughTime();
        String factoryName = throughRate.getFactoryName();
        ThroughRateExample example = new ThroughRateExample();
        example.createCriteria().andStatusEqualTo(Constants.NORMAL_STATUS).andThroughTimeEqualTo(date).andFactoryNameEqualTo(factoryName);
        int existCount = throughRateMapper.countByExample(example);
        if(existCount > 0){
            return "厂区："+factoryName+" "+date+"已经存在直通率！";
        }
        List<ThroughRateDetail> throughRateDetails = throughRate.getThroughRateDetails();
        if(CollectionUtils.isEmpty(throughRateDetails)){
            return "数据不完整！";
        }
        Map<String,Integer> gradeMap = Maps.newHashMap();
        for(ThroughRateDetail detail:throughRateDetails){
            if(gradeMap.containsKey(detail.getGradeValue())){
                return "不能存在相同规格！";
            }
            gradeMap.put(detail.getGradeValue(),1);
            detail.setRateId(id);
        }
        throughRate.setId(id);
        int count = throughRateMapper.insertSelective(throughRate);
        if(count < 1){
            return "插入数据失败！";
        }
        String resp = throughRateDetailService.addThroughRateDetails(throughRateDetails);
        if(StringUtils.isNotEmpty(resp)){
            throw new Exception(resp);
        }
        return resp;
    }

    @Override
    public String deleteThroughRateById(String id) throws Exception {
        if(StringUtils.isEmpty(id)){
            return "缺少主键！";
        }
        ThroughRate update = new ThroughRate();
        update.setStatus(Constants.DEL_STATUS);
        ThroughRateExample example = new ThroughRateExample();
        example.createCriteria().andIdEqualTo(id);

        int count = throughRateMapper.updateByExampleSelective(update,example);
        return CommonUtil.outStr(count);
    }

    @Override
    public List<ThroughRateVo> getThroughRateVoListByKb(QueryForm queryForm) throws Exception {
        ArrayList<String> list = new ArrayList<>();
        list.add("15");
        list.add("16");
        List<ThroughRateVo> results = Lists.newLinkedList();
        for (String factoryNames:list){
            queryForm.setFactoryName(factoryNames);
            String factoryName = queryForm.getFactoryName()+"-";
            String date = queryForm.getThroughTime();
            if(StringUtils.isEmpty(factoryName)||StringUtils.isEmpty(date)){
                return Lists.newArrayList();
            }
            List<ThroughRate> throughRates = throughRateMapper.selectThroughRateWithDetail(queryForm);
           if (throughRates.size()>0){
               Map<String,ThroughRateDetail> rateMap = Maps.newLinkedHashMap();
               rateMap.put("A",new ThroughRateDetail());
               rateMap.put("B",new ThroughRateDetail());
               rateMap.put("C",new ThroughRateDetail());
               rateMap.put("S",new ThroughRateDetail());
               rateMap.put("Y",new ThroughRateDetail());

               for(ThroughRate throughRate : throughRates){
                   List<ThroughRateDetail> details = throughRate.getThroughRateDetails();
                   if(CollectionUtils.isEmpty(details)){
                       continue;
                   }
                   String factory = throughRate.getFactoryName();
                   for(ThroughRateDetail rate : details){
                       String gradeValue = rate.getGradeValue();
                       ThroughRateVo vo = new ThroughRateVo();
                       vo.setCj(factory);
                       vo.setNames(Lists.newArrayList(gradeValue));

                       double dp1 = 0.00;
                       double dp2 = 0.00;
                       List<String> mas = Lists.newLinkedList();
                       mas.add(rate.getMa());
                       mas.add(rate.getExtend1());
                       dp1 +=Double.parseDouble(rate.getMa());
                       dp2 +=Double.parseDouble(rate.getExtend1());
                       dp1 +=Double.parseDouble(rate.getMb());
                       dp2 +=Double.parseDouble(rate.getExtend2());
                       dp1 +=Double.parseDouble(rate.getMc());
                       dp2 +=Double.parseDouble(rate.getExtend3());
                       dp1 +=Double.parseDouble(rate.getMd());
                       dp2 +=Double.parseDouble(rate.getExtend4());

                       if(rateMap.containsKey(gradeValue)){
                           ThroughRateDetail exist = rateMap.get(gradeValue);
                           if(StringUtils.isEmpty(exist.getGradeValue())){
                               exist.setGradeValue(rate.getGradeValue());
                               exist.setMa(rate.getMa());
                               exist.setMb(rate.getMb());
                               exist.setMc(rate.getMc());
                               exist.setMd(rate.getMd());
                               exist.setExtend1(rate.getExtend1());
                               exist.setExtend2(rate.getExtend2());
                               exist.setExtend3(rate.getExtend3());
                               exist.setExtend4(rate.getExtend4());
                           }else{
                               double addMa = Double.parseDouble(rate.getMa())+ Double.parseDouble(exist.getMa());
                               double addMb = Double.parseDouble(rate.getMb())+ Double.parseDouble(exist.getMb());
                               double addMc = Double.parseDouble(rate.getMc())+ Double.parseDouble(exist.getMc());
                               double addMd = Double.parseDouble(rate.getMc())+ Double.parseDouble(exist.getMd());
                               double addExtend1 = Double.parseDouble(rate.getExtend1())+ Double.parseDouble(exist.getExtend1());
                               double addExtend2 = Double.parseDouble(rate.getExtend2())+ Double.parseDouble(exist.getExtend2());
                               double addExtend3 = Double.parseDouble(rate.getExtend3())+ Double.parseDouble(exist.getExtend3());
                               double addExtend4 = Double.parseDouble(rate.getExtend4())+ Double.parseDouble(exist.getExtend4());
                               exist.setMa(addMa+"");
                               exist.setMb(addMb+"");
                               exist.setMc(addMc+"");
                               exist.setMd(addMd+"");
                               exist.setExtend1(addExtend1+"");
                               exist.setExtend2(addExtend2+"");
                               exist.setExtend3(addExtend3+"");
                               exist.setExtend4(addExtend4+"");
                           }
                       }

                       if(Double.parseDouble(rate.getMa()) == 0){
                           mas.add("0.00%");
                       }else{
                           double v1 = Double.parseDouble(rate.getMa());
                           double v2 = Double.parseDouble(rate.getExtend1());
                           String percent = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                           mas.add(percent);
                       }
                       vo.setMas(mas);

                       List<String> mbs = Lists.newLinkedList();
                       mbs.add(rate.getMb());
                       mbs.add(rate.getExtend2());
                       if(Double.parseDouble(rate.getMb()) == 0){
                           mbs.add("0.00%");
                       }else{
                           double v1 = Double.parseDouble(rate.getMb());
                           double v2 = Double.parseDouble(rate.getExtend2());
                           String percent = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                           mbs.add(percent);
                       }
                       vo.setMbs(mbs);

                       List<String> mcs = Lists.newLinkedList();
                       mcs.add(rate.getMc());
                       mcs.add(rate.getExtend3());
                       if(Double.parseDouble(rate.getMc()) == 0){
                           mcs.add("0.00%");
                       }else{
                           double v1 = Double.parseDouble(rate.getMc());
                           double v2 = Double.parseDouble(rate.getExtend3());
                           String percent = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                           mcs.add(percent);
                       }
                       vo.setMcs(mcs);

                       List<String> mds = Lists.newLinkedList();
                       mds.add(rate.getMd());
                       mds.add(rate.getExtend4());
                       if(Double.parseDouble(rate.getMd()) == 0){
                           mds.add("0.00%");
                       }else{
                           double v1 = Double.parseDouble(rate.getMd());
                           double v2 = Double.parseDouble(rate.getExtend4());
                           String percent = NumberUtil.getPoint(v2*100.0/v1,2)+"%";
                           mds.add(percent);
                       }
                       vo.setMds(mds);

                       if(dp1 == 0){
                           vo.setDp("0.00%");
                       }else{
                           String dpPercent = NumberUtil.getPoint(dp2*100.0/dp1,2)+"%";
                           vo.setDp(dpPercent);
                       }
                       results.add(vo);
                   }
               }
           }
        }

        return results;
    }
}
